﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CUREBusinessLogicLayer;
using CURE.Handler;


namespace CURE.SystemManagement
{
    public partial class SystemAuthority : System.Web.UI.Page
    {
        AuthorityBll authoritybll = new AuthorityBll();
        EventLogBll eventlogbll = new EventLogBll();
        protected void Page_Load(object sender, EventArgs e)
        {
            JudgeAuthority ja = new JudgeAuthority();
            if (ja.JudgeUserAuthorityByGroupId("SystemManagement/AuthorityManagement.aspx"))
            {

                string action = Request.QueryString["action"];
                string name = Request.QueryString["name"];
                string info = Request.QueryString["info"];
                string masterid = string.Empty;//当前登录用户
                if (HttpContext.Current.Request.Cookies["MWS_User"] != null)
                {
                    masterid = HttpContext.Current.Request.Cookies["MWS_User"]["ID"];
                }
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add("name", name);
                dic.Add("info", info);


                if (action != null && action != "")
                {
                    if (name != null && name != "")
                    {
                        if (action == "add")//新增
                        {
                            authoritybll.addGroupData(dic);
                            #region 插入日志
                            Dictionary<string, object> eventlogdic = new Dictionary<string, object>();
                            eventlogdic.Add("masterid", masterid);
                            eventlogdic.Add("msg", "新增用户组：" + name + "");
                            eventlogbll.InsertEventLog(eventlogdic);
                            #endregion
                        }
                    }
                    if (name != null && name != "")
                    {
                        if (action == "edit")//编辑
                        {
                            authoritybll.UpdateGroupData(dic);
                            #region 插入日志
                            Dictionary<string, object> eventlogdic = new Dictionary<string, object>();
                            eventlogdic.Add("masterid", masterid);
                            eventlogdic.Add("msg", "编辑用户组：" + name + "");
                            eventlogbll.InsertEventLog(eventlogdic);
                            #endregion
                        }
                    }
                    if (name != null && name != "" && name != "0")
                    {
                        if (action == "delete")//删除
                        {
                            string groupname = authoritybll.GetGroupNameByInfo(name);
                            authoritybll.DeleteGroupData(dic);
                            #region 插入日志
                            Dictionary<string, object> eventlogdic = new Dictionary<string, object>();
                            eventlogdic.Add("masterid", masterid);
                            eventlogdic.Add("msg", "删除用户组：" + groupname + "");
                            eventlogbll.InsertEventLog(eventlogdic);
                            #endregion
                        }
                    }
                }
                GetGroupData();
            }
            else
            {
                Response.Write("<script language=javascript>top.location.href='../../Login.aspx';</script>");
                Response.End();
            }
        }

        /// <summary>
        /// 获取用户组信息，给RepeaterGroup绑定数据源
        /// </summary>
        public void GetGroupData()
        {
            DataTable dt = new DataTable();
            dt = authoritybll.GetGroupData();
            if (dt != null && dt.Rows.Count > 0)
            {
                RepeaterGroup1.DataSource = dt;
                RepeaterGroup1.DataBind();
                RepeaterGroupList1.DataSource = dt;
                RepeaterGroupList1.DataBind();
            }
        }
    }


}
